package view
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	
	import data.ArmsProp;
	import data.Globals;
	
	import events.EventType;
	import events.GameDispatherEvent;
	import events.GameEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import interfaces.IView;
	
	import manager.ObjDataManage;
	import manager.TankName;
	
	import utils.GameUtils;
	
	import view.base.BaseView;
	import view.other.GameInfoView;
	import view.tip.ArmsTip;
	
	public class GoodMenuView extends BaseView implements IView
	{
		private var goodMenu:GoodMenu;
		
		private var armsTip:ArmsTip;
		
		private var scoreView:GameInfoView;
		
		private var goodContainer:Sprite;
		
		private var armsAry:Object;
		
		private var _curTankType:String;
		
		/**
		 *弹出后用掉的时间 
		 */
		private var useTime:Number = 0;
		
		private var yaluji:YaLuJi;
		
		private var _isPause:Boolean;
		
		public function GoodMenuView()
		{
			super();
			this.x = Globals.stage_width/2;
			this.y = -36;
			goodMenu = new GoodMenu();
			this.addChild(goodMenu);
			
			goodContainer = new Sprite();
			this.addChild(goodContainer);
			
			armsTip = new ArmsTip();
			
			scoreView = new GameInfoView();
			this.addChild(scoreView);
			scoreView.score = Globals.initialEnergy;
			
			goodContainer.addEventListener(MouseEvent.MOUSE_DOWN,chooseArms);
	    	goodContainer.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
			goodContainer.addEventListener(MouseEvent.MOUSE_OVER,over);
			goodContainer.addEventListener(MouseEvent.MOUSE_OUT,out);
			this.addEventListener(MouseEvent.MOUSE_OVER,show);
			GameDispatherEvent.addEventListener(EventType.MOUSE_UP,cancelArms);
			isCloseLoop = true;
			goodMenu.startBtn.stop();
			goodMenu.startBtn.buttonMode = true;
			goodMenu.home.addEventListener(MouseEvent.CLICK,homeHandle);
			goodMenu.startBtn.addEventListener(MouseEvent.CLICK,pauseOrStartHandle);
		}
		
		protected function homeHandle(event:MouseEvent):void
		{
			Globals.towerWar.returnLogin();
		}
		
		protected function pauseOrStartHandle(event:MouseEvent):void
		{
			isPause = !isPause;
		}
		
		public function setWave(cur:int,total:int):void{
			scoreView.setWave(cur,total);
		}
		
		protected function cancelArms(event:GameEvent):void
		{
			curTankType = null;
		}
		
		private function mouseUp(e:MouseEvent):void{
			trace("mouseUp");
		}
		
		private function refreshGood():void{
			var len:int = goodContainer.numChildren;
			for(var i:int=0;i<len;i++){
				var mv:MovieClip = goodContainer.getChildAt(i) as MovieClip;
				var tp:ArmsProp = ObjDataManage.getInstance().getTankProp(mv.name);
				if(tp.revive>scoreView.score){
					GameUtils.setGrayFiler(mv);
					mv.mouseChildren = false;
					mv.mouseEnabled = false;
				}else{
					GameUtils.clearGrayFiler(mv);
					mv.mouseChildren = true;
					mv.mouseEnabled = true;
				}
			}
		}
		
		public function calculateScore(value:int):void{
			scoreView.score += value;
			refreshGood();
		}
		
		override protected function loop(event:Event):void{
			if(isCloseLoop)return;
			useTime++;
			if(useTime>30*5){
				if(!this.hitTestPoint(this.stage.mouseX,this.stage.mouseY)){
					hide();
					isCloseLoop = true;
				}
			}
		}
		
		private function out(event:MouseEvent):void
		{
			if(event.target == goodContainer) return;
			event.target.filters = null;
			this.removeChild(armsTip);
		}
		
		private function over(event:MouseEvent):void
		{
			if(event.target == goodContainer) return;
			var filter:GlowFilter = new GlowFilter(0x667BD4);
			var clip:MovieClip = event.target as MovieClip;
			clip.filters = [filter];
			this.addChild(armsTip);
			armsTip.x = mouseX+2;
			armsTip.y = mouseY+2;
			armsTip.show(event.target.name);
		}
		
		public function initface():void
		{
			armsAry = ObjDataManage.getInstance().tankMap;
			var key:String;
			var i:int = 0;
			var tank:MovieClip;
			for(key in armsAry){
				var tp:ArmsProp = armsAry[key]; 
				var cls:Class = TankName.getTankClsForClip(tp.name);
				tank = new cls();
				tank.x = -157+i*52.2;
				tank.y = 5;
				tank.scaleX = 40/tank.height;
				tank.height = 40;
				tank.name = tp.name;
				tank.buttonMode = true;
				goodContainer.addChild(tank);
				i++;
			}
			
			scoreView.score = Globals.initialEnergy;
		}
		
		public function show(e:MouseEvent = null):void{
			TweenLite.to(this, 1, {x:this.x, y:27, ease:Back.easeOut});
			isCloseLoop = false;
		}
		
		public function hide():void{
			TweenLite.to(this, 1, {x:this.x, y:-20, ease:Back.easeOut});
		}
		
		private function chooseArms(event:MouseEvent):void
		{
			if(event.target != goodContainer&&curTankType==null){
				curTankType = event.target.name;
				Globals.gameView.showMouseTip(curTankType);
			}
		}
		
		public function clearView():void{
			while(goodContainer.numChildren){
				goodContainer.removeChildAt(0);
			}
			isCloseLoop = true;
		}

		public function get curTankType():String
		{
			return _curTankType;
		}

		public function set curTankType(value:String):void
		{
			_curTankType = value;
		}

		public function get isPause():Boolean
		{
			return _isPause;
		}

		public function set isPause(value:Boolean):void
		{
			_isPause = value;
			Globals.towerWar.isPauseGame = isPause;
			goodMenu.startBtn.gotoAndStop(int(isPause)+1);
		}

	}
}